﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Threading;
using System.Windows.Input;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Windows.Interop;

//http://apocryph.org/2006/09/10/wtf_is_wrong_with_wpf_focus/


namespace Bezel__.Windows {
    static class FocusHelper {

		//http://www.pinvoke.net/default.aspx/user32/SetWindowPos.html

		[DllImport("user32.dll")]
		public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
		[DllImport("user32.dll")]
		public static extern bool BringWindowToTop(IntPtr hWnd);
		[DllImport("User32.dll")]
		public static extern Int32 SetForegroundWindow(IntPtr hWnd);
		[DllImport("user32.dll")]
		static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

		public const int SWP_ASYNCWINDOWPOS = 0x4000;
		public const int SWP_DEFERERASE = 0x2000;
		public const int SWP_DRAWFRAME = 0x0020;
		public const int SWP_FRAMECHANGED = 0x0020;
		public const int SWP_HIDEWINDOW = 0x0080;
		public const int SWP_NOACTIVATE = 0x0010;
		public const int SWP_NOCOPYBITS = 0x0100;
		public const int SWP_NOMOVE = 0x0002;
		public const int SWP_NOOWNERZORDER = 0x0200;
		public const int SWP_NOREDRAW = 0x0008;
		public const int SWP_NOREPOSITION = 0x0200;
		public const int SWP_NOSENDCHANGING = 0x0400;
		public const int SWP_NOSIZE = 0x0001;
		public const int SWP_NOZORDER = 0x0004;
		public const int SWP_SHOWWINDOW = 0x0040;

		public static readonly IntPtr HWND_TOP = new IntPtr(0);
		public static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
		public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
		public static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
		//

		[DllImport("user32.dll")]
		static extern IntPtr SetActiveWindow(IntPtr hWnd);

        private delegate void MethodInvoker();

		public static void sendWindowToFront(Window win) {

			IntPtr hWnd = new WindowInteropHelper(win).Handle;

			uint TOPMOST_FLAGS = SWP_NOMOVE | SWP_NOSIZE | SWP_ASYNCWINDOWPOS;
			bool success = SetWindowPos(hWnd, HWND_TOP, 0, 0, 0, 0, TOPMOST_FLAGS);

		}

		public static void sendWindowToBack(Window win) {

			IntPtr hWnd = new WindowInteropHelper(win).Handle;

			uint TOPMOST_FLAGS = SWP_NOMOVE | SWP_NOSIZE | SWP_ASYNCWINDOWPOS;
			
			// send to back
			bool success = SetWindowPos(hWnd, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);

			// move focus to topmost window
			IntPtr bottomWin = GetWindow(IntPtr.Zero, (uint)GetWindow_Cmd.GW_HWNDFIRST);
			SetActiveWindow(bottomWin);
		}




		[DllImport("user32.dll", SetLastError = true)]
		static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);

		enum GetWindow_Cmd : uint {
			GW_HWNDFIRST = 0,
			GW_HWNDLAST = 1,
			GW_HWNDNEXT = 2,
			GW_HWNDPREV = 3,
			GW_OWNER = 4,
			GW_CHILD = 5,
			GW_ENABLEDPOPUP = 6
		}
    }
}
